﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System.Windows;
using System.Windows.Input;
using Peter.Logic;

namespace Peter.View
{
   /// <summary>
   /// Interaction logic for ViewTerminal.xaml
   /// </summary>
   public partial class ViewTerminal
   {
      private ModelTerminal m_ViewModel;

      /// <summary>
      /// Initializes a new view for the terminal.
      /// </summary>
      public ViewTerminal ()
      {
         InitializeComponent ();
         this.DataContextChanged += this.OnDataContextChanged;
      }

      /// <summary>
      /// Occurs when the data context changes.
      /// </summary>
      /// <param name="sender">This</param>
      /// <param name="e">DependencyPropertyChangedEventArgs</param>
      private void OnDataContextChanged (object sender, DependencyPropertyChangedEventArgs e)
      {
         this.m_ViewModel = this.DataContext as ModelTerminal;
         if (this.m_ViewModel != null)
         {
            this.DataContextChanged -= this.OnDataContextChanged;
            this.m_ViewModel.SetEditor (this._TextEditor);
         }
      }

      /// <summary>
      /// Occurs when a key is pressed in the command combo box.
      /// </summary>
      /// <param name="sender">Command combo box.</param>
      /// <param name="e">KeyEventArgs</param>
      private void ComboBoxPreviewKeyDown (object sender, KeyEventArgs e)
      {
         if (this.m_ViewModel != null)
         {
            if (e.Key == Key.Tab)
            {
               this.m_ViewModel.CompleteCommandLine ();
               e.Handled = true;
            }
            else
            {
               this.m_ViewModel.ClearCommandLineComplete ();
               if (e.Key == Key.Enter)
               {
                  this.m_ViewModel.RunCommandLine ();
               }
            }
         }
      }
   }
}
